/**
 * Por Joshua
 * Descripcion o proposito
 * @class namespace
 *
 * @extends clasePadre
 * @autor Ing. Josue Hernandez Gomez<br>
 *        joshua@extjs.mx<br>
 *        http://www.joshua.com.mx
 * @fecha 12/9/12 1:34 AM. Mexico DF
 */



joshua.embarques.winFinalizar= Ext.extend(Ext.Window,{
	layout      : 'fit',
	width: 270,
	height: 500,
	closeAction :'close',
	resizable:false,
	maximizable:false,
	modal:true,
	buttonAlign:'center',
	iconCls:'save',
	layout:'border',
	title:'Almacenar embarque',
    
	initComponent: function(){
		this.panelform=new Ext.form.FormPanel({
			frame:false,
            border: false,
            style: 'padding: 5px;',
			region:'north',
			labelWidth:50,
			height:65,
			items:[{
				xtype:'datefield',
				fieldLabel: 'Fecha',
				format:'Y-m-d',
				width:120,
				emptyText:'De embarque',
				name: 'txt_fembarque',
				allowBlank: false,
                value: new Date()
			},
			{
				xtype:'comboAereolinea',
				fieldLabel: 'Via',
				width:120,
                value:2,
				emptyText:'Aereolinea...',
				name: 'txt_aeroembarque',
				allowBlank: false
			}]
		});


        var fields = [
            {name:'costo_total', type: 'string'},
            {name:'prefijo', type: 'string'},
            {name:'id', type: 'string',mapping : 'id'},
            {name:'estatus', type: 'string'},
            {name:'id_envio', type: 'string'},
            {name:'fecha', type: 'string'},
            {name:'remitente', type: 'string'},
            {name:'destinatario', type: 'string'},
            {name:'completo', type: 'string'},
            {name:'peso', type: 'string'}
        ];


	// Column Model shortcut array

        // declare the source Grid
        this.firstGrid = new Ext.grid.EditorGridPanel({
            store            : this.secondGridStore,//------------------------------------------------------------------CAMBIAR CUANDO YA SE AGREGUE A LA VENTANA
            columns          : [
                    new Ext.grid.RowNumberer(),
                    {header: joshua.util.icono.boleto+' Guia', iconCls:'usuario_borrar', dataIndex: 'id', sortable: true,renderer:this.renderGuia},
                    {header: joshua.util.icono.agregar+' Completo', dataIndex: 'completo', sortable: true,renderer:this.renderParcialRojo,
                        editor: new Ext.form.ComboBox({
                            store: [['Completo', 'Completo'], ['Parcial', 'Parcial']],
                            typeAhead: true,
                            triggerAction: 'all',
                            selectOnFocus: true,
                            allowBlank: true,
                            editable: false
                        })
                    }
                ],
            frame:true,
            border: false,
            iconCls:'package_go',
            clicksToEdit:1,
            stripeRows  : true,
            layout:'fit',
            height:400,
            region           : 'center',
            title            : 'Envios para embarque',
            listeners:{
                afteredit: function (e){
                    e.record.commit();
                }
            }
        });

		Ext.apply(this,{
            manager: Ext.managerEnlace,
			items:[this.panelform,this.firstGrid],
			buttons:[{
                text:'<b>Guardar</b>',
                iconCls:'save',
                scope:this,
                handler: function(){
                    this.guardarForm();
                }
            },{
                text:'<b>Cancelar</b>',
                iconCls:'delete',
                scope:this,
                handler:function(){
                    this.hide();
                }
            }]
		});
		joshua.embarques.winFinalizar.superclass.initComponent.apply(this,arguments);
	},


	renderGuia: function (data,cell,record,index,columnindex,store){
		return record.data.prefijo+"-"+data;
	},
    
	renderParcialRojo: function (data,cell,record,index,columnindex,store){
		if(data=="Parcial")
			return '<div style="color:#ff0000">'+data+"</div>";
		else return data;
	},

	guardarForm:function(o){
		//establecemos el md5
		if (this.panelform.getForm().isValid()) {
			var w=this;

			w.envios= new Array();
			w.firstGrid.store.each(function (record){
				w.envios.push({id:record.data.id,c:record.data.completo})
			},w);

			this.panelform.getForm().submit({
				url:"Servicios/s.php",
				params:{
					cmd:'insertar',
					canasta:Ext.util.JSON.encode(w.envios),
					className:'Embarque'
				},
				success:function (form,action){
 					var respuesta=Ext.util.JSON.decode(action.response.responseText);
					joshua.embarques.imprimir(respuesta.id);

					w.hide();
					w.firstGrid.store.removeAll();

					w.hide();///----------------------------------------------------------------------------CAMBIAR AL CAMBIAR LA VENTANA
					w.secondGridStore.removeAll();
					w.firstGridStore.reload( );
					w.labelpeso.setText("0Kg/0Lb");

					//volver a listar los embarques
					joshua.embarques.grid.store.reload({params:{start: 0,limit:joshua.PAGINA_CUANTOS}});//, condiciones:_condiciones}});
				},
				waitMsg:'Guardando...'
			});
		}else
			joshua.funciones.alert('aviso','Aviso',"Faltan datos por completar o algunos no son validos");

 	}//eo guardarForm
});
